home *** CD-ROM | disk | FTP | other *** search
- /***
- * CMessenger
- *
- * High level ADSP handler
- *
- * this object handles data stream connections over appletalk
- *
- * Superclass: CADSP
- *
- * Copyright © 1992 Bernard Bernstein. All rights reserved.
- ***/
-
- #include <Global.h>
-
- #include "CMessenger.h"
-
- #define QSIZE 600
- #define DATASIZE 128
-
- /***
- * IMessenger
- *
- * initialize the messenger
- ***/
- void CMessenger::IMessenger(void)
- {
- CNBP *anNBP = nil;
-
- itsSocket = 0;
- waitingForConnect = false;
- named = false;
-
- // make an NBP handler for name binding
- anNBP = new CNBP;
- anNBP->INBP();
- itsNBP = anNBP;
-
- anNBP->SetSelfSend(true);
-
- CADSP::IADSP(QSIZE, DATASIZE);
-
- }
-
-
- /***
- * Dispose
- *
- * Kill the messenger
- ***/
- void CMessenger::Dispose(void)
- {
- KillConnector();
- ForgetObject(itsNBP);
-
- inherited::Dispose();
- }
-
-
-
-
-
- /**********************************************************************
-
- Accessing information
-
- **********************************************************************/
-
-
- /***
- * GetSocket
- *
- * return the socket number for this session
- ***/
- short CMessenger::GetSocket(void)
- {
- return itsSocket;
- }
-
- /***
- * GetRemoteAddr
- *
- * return the AddrBlock of the remote connection which contacted
- * this server.
- ***/
- AddrBlock CMessenger::GetRemoteAddr(void)
- {
- return itsRemoteAddr;
- }
-
-
- /***
- * GetNBP
- *
- * return the nbp handler
- ***/
- CNBP *CMessenger::GetNBP(void)
- {
- return itsNBP;
- }
-
-
-
-
- /***
- * SetSocket
- *
- * set the socket for this stream
- ***/
- void CMessenger::SetSocket(short aSocket)
- {
- itsSocket = aSocket;
- itsNBP->SetSocket(aSocket);
- }
-
- /***
- * SetRemoteAddr
- *
- * set the remote address for this stream
- ***/
- void CMessenger::SetRemoteAddr(AddrBlock anAddr)
- {
- itsRemoteAddr = anAddr;
- }
-
-
-
-
- /**********************************************************************
-
- Name binding stuff
-
- **********************************************************************/
-
- /***
- * SetName
- *
- * Provide a name for this connection.
- * Needs an object name, type name and zone name
- ***/
- void CMessenger::SetName(Str32 objName, Str32 typeName, Str32 zoneName)
- {
- ASSERT(itsNBP);
-
- itsNBP->SetName(objName, typeName, zoneName);
- named = true;
- }
-
- /***
- * PostName
- *
- * Post the NBP name for the socket. A socket must have been established first.
- ***/
- void CMessenger::PostName(void)
- {
- ASSERT(itsNBP);
- ASSERT(itsSocket != 0);
-
- // register this socket with NBP
- itsNBP->Register();
- }
-
- /***
- * RemoveName
- *
- * Remote the NBP name
- ***/
- void CMessenger::RemoveName(void)
- {
- ASSERT(itsNBP);
- itsNBP->Remove();
- named = false;
- }
-
-
-
-
- /**********************************************************************
-
- Starting and Stopping a connection
-
- **********************************************************************/
-
-
- /***
- * MakeConnector
- *
- * make a socket which can be used for connecting to another socket.
- *
- * The messenger must be named before this method will work.
- ***/
- void CMessenger::MakeConnector(void)
- {
- short aSocket = itsSocket;
-
- ASSERT(named);
-
- DSPInit(&aSocket); // if itsSocket was 0, it will be reassigned
- SetSocket(aSocket);
-
- PostName();
- }
-
-
- /***
- * KillConnector
- *
- * remove the connector completely
- ***/
- void CMessenger::KillConnector(void)
- {
- RemoveName();
- DSPRemove();
- }
-
-
- /***
- * OpenSession
- *
- * open an active session with another socket connector
- * the AddrBlock of the other session is passed in here
- *
- ***/
- void CMessenger::OpenSession(AddrBlock addr)
- {
- // ••• do i have to close it if one is already open passively?
- waitingForConnect = false;
- DSPOpen(addr, ocRequest);
- SetRemoteAddr(addr);
- }
-
-
- /***
- * WaitForSession
- *
- * open a passive session. waiting for another socket
- * to request a connection.
- *
- * *** This call is made synchronously ***
- *
- * after calling this method, you must periodically call:
- *
- *
- ***/
- void CMessenger::WaitForSession(void)
- {
- AddrBlock filter;
-
- filter.aNet = 0;
- filter.aNode = 0x00;
- filter.aSocket = 0x00;
-
- DSPOpen(filter, ocPassive);
- waitingForConnect = true;
- }
-
- /***
- * StillWaitingForSession
- *
- * Call this periodically after calling WaitForSession.
- * if a session has been requested, it will return true.
- *
- ***/
- Boolean CMessenger::StillWaitingForSession(void)
- {
- OSErr err;
-
- if (waitingForConnect && ((err = DSPioResult()) != 1))
- {
- waitingForConnect = false;
- if (err != 1)
- {
- FailOSErr(err);
- }
- }
-
- return waitingForConnect;
- }
-
-
- /***
- * CloseSession
- *
- * Close the session
- ***/
- void CMessenger::CloseSession(void)
- {
- DSPClose();
- waitingForConnect = false;
- }
-
-
- /**********************************************************************
-
- Maintaining a connection
-
- **********************************************************************/
-
-
- /***
- * ReceiveMessage
- *
- * Read some data from the connection
- * This should be done whenever MessageWaiting returns true
- ***/
- void CMessenger::ReceiveMessage(void *message, short amountToRead, short *amountRead)
- {
- DSPRead(message, amountToRead, amountRead);
- }
-
-
- /***
- * SendMessage
- *
- * Send some data across the connection
- ***/
- void CMessenger::SendMessage(void *message, short size)
- {
- DSPWrite(message, size);
- }
-
-
- /***
- * MessageWaiting
- *
- * Is there a message waiting?
- * If this is true, then do a ReceiveMessage to get it.
- * This should be called periodically to handle incoming messages.
- ***/
- Boolean CMessenger::MessageWaiting(void)
- {
- short sendPend, sendAvail, recvPend, recvAvail;
-
- DSPStatus(&sendPend, &sendAvail, &recvPend, &recvAvail);
-
- return (recvPend > 0);
- }
-
-
-